﻿using System;
using EventMgrLib;
using log4net;

namespace SmartMotion.Core.Helpers
{
    public sealed class Log
    {
        #region 属性单例模式

        private static readonly Lazy<Log> Instance = new Lazy<Log>(() => new Log());

        private Log()
        {
            EventMgr.Ins.GetEvent<DebugMessage>().Subscribe(SaveDebugMessage);
        }

        ~Log()
        {
            try
            {
                EventMgr.Ins.GetEvent<DebugMessage>().Unsubscribe(SaveDebugMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
           
        }
        private void SaveDebugMessage(string obj)
        {
            Debug(obj);
        }

        public static Log Ins { get; } = Instance.Value;

        #endregion 属性单例模式

        public void Error(object message,string logerName="SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Error(message);
        }

        public void Error(object message, Exception exception,string logerName="SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Error(message,exception);
        }

        public void Fatal(object message, Exception exception, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Fatal(message, exception);
        }

        public void Fatal(object message, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Fatal(message);
        }

        public void Info(object message, Exception exception, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Info(message, exception);
        }
            
        public void Info(object message, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Info(message);
        }

        public void Debug(object message, Exception exception, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Debug(message, exception);
        }

        public void Debug(object message, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Debug(message);
        }

        public void Warn(object message, Exception exception, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Warn(message, exception);
        }

        public void Warn(object message, string logerName = "SmatMotion")
        {
            var logger = LogManager.GetLogger(logerName);
            logger.Warn(message);
        }
    }
}